//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Abcd.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Interpolation.h>
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (Double a, Double b, Double c, Double d, Boolean aIsFixed, Boolean bIsFixed, Boolean cIsFixed, Boolean dIsFixed, Microsoft::FSharp::Core::FSharpOption<Boolean>^ vegaWeighted, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>^ optMethod) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CEndCriteria^ _CendCriteria;
    COptimizationMethod^ _CoptMethod;
    try
    {
#ifdef HANDLE
        _phAbcd = NULL;
#endif
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b);
        QuantLib::Real _c = (QuantLib::Real)ValueHelper::Convert (c);
        QuantLib::Real _d = (QuantLib::Real)ValueHelper::Convert (d);
        bool _aIsFixed = (bool)ValueHelper::Convert (aIsFixed);
        bool _bIsFixed = (bool)ValueHelper::Convert (bIsFixed);
        bool _cIsFixed = (bool)ValueHelper::Convert (cIsFixed);
        bool _dIsFixed = (bool)ValueHelper::Convert (dIsFixed);
        bool _vegaWeighted = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (vegaWeighted) ? (bool)ValueHelper::Convert (vegaWeighted->Value) : false); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>::IsSome::get (endCriteria))
        {
            _CendCriteria = safe_cast<CEndCriteria^> (endCriteria->Value);
            _CendCriteria->Lock();
        }
        boost::shared_ptr<QuantLib::EndCriteria> _endCriteria = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>::IsSome::get (endCriteria) ? static_cast<boost::shared_ptr<QuantLib::EndCriteria>> (_CendCriteria->GetShared ()) : boost::shared_ptr<QuantLib::EndCriteria>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>::IsSome::get (optMethod))
        {
            _CoptMethod = safe_cast<COptimizationMethod^> (optMethod->Value);
            _CoptMethod->Lock();
        }
        boost::shared_ptr<QuantLib::OptimizationMethod> _optMethod = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>::IsSome::get (optMethod) ? static_cast<boost::shared_ptr<QuantLib::OptimizationMethod>> (_CoptMethod->GetShared ()) : boost::shared_ptr<QuantLib::OptimizationMethod>()); //1
        _ppAbcd = new boost::shared_ptr<QuantLib::Abcd> (new QuantLib::Abcd ( _a,  _b,  _c,  _d,  _aIsFixed,  _bIsFixed,  _cIsFixed,  _dIsFixed,  _vegaWeighted,  _endCriteria,  _optMethod ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CendCriteria != nullptr) _CendCriteria->Unlock();
        if (_CoptMethod != nullptr) _CoptMethod->Unlock();
    }
}
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (boost::shared_ptr<QuantLib::Abcd>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcd = NULL;
#endif
	_ppAbcd = &childNative;
    
}
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (QuantLib::Abcd& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAbcd = NULL;
#endif
	_ppAbcd = new boost::shared_ptr<QuantLib::Abcd> (&childNative);
    
    _AbcdOwner = owner;
    
}

Cephei::QL::Math::Interpolations::CAbcd::CAbcd (CAbcd^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcd = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAbcd = new boost::shared_ptr<QuantLib::Abcd> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcd = NULL;
#endif
	if (!t->IsSubclassOf(CAbcd::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (QuantLib::Handle<QuantLib::Abcd>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAbcd = &childNative;
	_ppAbcd = &static_cast<boost::shared_ptr<QuantLib::Abcd>>(childNative.currentLink());
    
    _AbcdOwner = owner;
}
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (QuantLib::Handle<QuantLib::Abcd> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAbcd = &childNative;
	_ppAbcd = &static_cast<boost::shared_ptr<QuantLib::Abcd>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CAbcd::CAbcd (QuantLib::Abcd childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcd = NULL;
#endif
	_ppAbcd = new boost::shared_ptr<QuantLib::Abcd> (new QuantLib::Abcd (childNative));
    
}
#endif

Cephei::QL::Math::Interpolations::CAbcd::~CAbcd ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAbcd != NULL)
    {
	    delete _ppAbcd;
        _ppAbcd = NULL;
    }
}
Cephei::QL::Math::Interpolations::CAbcd::!CAbcd ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAbcd != NULL)
    {
	    delete _ppAbcd;
    }
}
QuantLib::Abcd& Cephei::QL::Math::Interpolations::CAbcd::GetReference ()
{
    if (_ppAbcd == NULL) throw gcnew NativeNullException ();
	return **_ppAbcd;
}
boost::shared_ptr<QuantLib::Abcd>& Cephei::QL::Math::Interpolations::CAbcd::GetShared ()
{
    if (_ppAbcd == NULL) throw gcnew NativeNullException ();
	return *_ppAbcd;
}
QuantLib::Abcd* Cephei::QL::Math::Interpolations::CAbcd::GetPointer ()
{
    if (_ppAbcd == NULL) throw gcnew NativeNullException ();
	return &**_ppAbcd;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Abcd>& Cephei::QL::Math::Interpolations::CAbcd::GetHandle ()
{
	if (_phAbcd == NULL)
	{
		_phAbcd = new Handle<QuantLib::Abcd> (*_ppAbcd);
	}
	return *_phAbcd;
}
#endif
bool Cephei::QL::Math::Interpolations::CAbcd::HasNative () 
{
	return (_ppAbcd != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::IAbcd^ Cephei::QL::Math::Interpolations::CAbcd_Factory::Create (Double a, Double b, Double c, Double d, Boolean aIsFixed, Boolean bIsFixed, Boolean cIsFixed, Boolean dIsFixed, Microsoft::FSharp::Core::FSharpOption<Boolean>^ vegaWeighted, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>^ optMethod)
{
    return gcnew CAbcd ( a,  b,  c,  d,  aIsFixed,  bIsFixed,  cIsFixed,  dIsFixed,  vegaWeighted,  endCriteria,  optMethod);
}
